'use strict';

/**
 * Config for the router
 */
angular.module('app')
    .run(
        ['$rootScope', '$state', '$stateParams', '$http',
            function ($rootScope, $state, $stateParams, $http) {
                $rootScope.$state = $state;
                $rootScope.$stateParams = $stateParams;
            }
        ]
    )
    .config(
        ['$stateProvider', '$urlRouterProvider',
            function ($stateProvider, $urlRouterProvider) {

                $urlRouterProvider
                    .otherwise('/access/login');
                $stateProvider
                    .state('app', {
                        abstract: true,
                        url: '/app',
                        templateUrl: 'tpl/app.html',
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load([
                                        'js/services/book/book.js',
                                        'js/services/check/checkStatus.js',
                                        'js/services/modal/bookListModal.js',
                                        'js/controllers/modal/bookListModal.js',
                                        'js/controllers/nav/nav.js',
                                        'js/services/object/objectUtils.js',
                                        'js/services/tree/treeUtils.js',
                                        'js/services/utils/stringUtils.js',
                                        'js/services/utils/phoneUtils.js',
                                        'js/services/utils/downloadUtils.js',
                                        'js/filters/UTCTime.js'
                                    ]);
                                }]
                        }
                    })

                    // book
                    .state('app.book', {
                        controller: 'bookBaseCtrl',
                        url: '/book',
                        template: '<div ui-view></div>',
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/services/book/book.js',
                                        'js/controllers/book/bookBase.js',
                                        'js/controllers/book/bookList.js',
                                        'js/controllers/book/bookAdd.js',
                                        'js/controllers/book/bookAddFromNet.js',
                                        'js/controllers/book/bookUpdate.js']);
                                }]
                        }
                    })
                    .state('app.book.list', {
                        controller: 'bookListCtrl',
                        url: '/list',
                        templateUrl: 'tpl/book/bookList.html'
                    })
                    .state('app.book.add', {
                        controller: 'bookAddCtrl',
                        url: '/add',
                        templateUrl: 'tpl/book/bookInfo.html',
                        resolve: {
                            translate: '$translate',
                            status: function (translate) {
                                return {
                                    title: translate.instant('strings.title.new.book')
                                }
                            }
                        }
                    })
                    .state('app.book.addFromNet', {
                        controller: 'bookAddFromNetCtrl',
                        url: '/addFromNet',
                        templateUrl: 'tpl/book/bookAddFromNet.html',
                        resolve: {
                            translate: '$translate',
                            status: function (translate) {
                                return {
                                    title: translate.instant('strings.title.new.book')
                                }
                            }
                        }
                    })
                    .state('app.book.update', {
                        controller: 'bookUpdateCtrl',
                        url: '/update?id',
                        templateUrl: 'tpl/book/bookInfo.html',
                        resolve: {
                            translate: '$translate',
                            status: function (translate) {
                                return {
                                    title: translate.instant('strings.title.update.book')
                                }
                            },
                            book: ['bookService', '$stateParams', function (bookService, $stateParams) {
                                return bookService.getById($stateParams.id);
                            }]
                        },
                        params: {
                            id: null
                        }
                    })

                    .state('access', {
                        url: '/access',
                        template: '<div ui-view class="fade-in-right-big smooth"></div>'
                    })
                    .state('access.login', {
                        url: '/login',
                        templateUrl: 'tpl/page_login.html',
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/controllers/login.js']);
                                }]
                        }
                    })
                    .state('access.signUp', {
                        url: '/signUp',
                        templateUrl: 'tpl/page_signUp.html',
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/controllers/signUp.js']);
                                }]
                        }
                    })
                    .state('access.forgotpwd', {
                        url: '/forgotpwd',
                        templateUrl: 'tpl/page_forgotpwd.html'
                    })
                    .state('access.404', {
                        url: '/404',
                        templateUrl: 'tpl/page_404.html'
                    });

            }
        ]
    );